package com.quest.space.handler;

import com.quest.space.MainWindow;
import com.quest.space.data.MainContext;
import com.quest.space.data.enums.EventEnum;
import com.quest.space.data.enums.FuncEnum;
import com.quest.space.poker.biz.impl.GameServiceImpl;
import com.quest.space.poker.dto.PlayerDto;

import javax.swing.*;

import static com.quest.space.data.DataCenter.functionList;
import static com.quest.space.data.UserCache.eventEnum;
import static com.quest.space.handler.PokerHandler.handlePoker;
import static com.quest.space.handler.PokerHandler.sendToServer;
import static com.quest.space.handler.UserHandler.login;
import static com.quest.space.handler.UserHandler.showMenu;

public class EventHandler {
    public static void submitEvent() {
        switch (eventEnum){
            case LOGIN:
                login();
                break;
            case REGISTER:
                break;
            case MAIN_MENU:
                chooseMainMenu();
                break;
            case POKER:
                handlePoker();
            default:
        }
    }

    public static void chooseMainMenu() {
        MainContext context = MainWindow.mainContext;
        JTextArea textArea3 = context.getTextArea3();
        if (FuncEnum.ROBOT_POKER.getCode().equals(textArea3.getText())) {
            handleRobotPoker();
        }else {
            textArea3.setText(null);
            context.getSysArea().setText("input error, you can choose\n" + functionList);
        }
    }

    public static void handleRobotPoker() {
        MainContext context = MainWindow.mainContext;
        //调用开始游戏接口
        JTextArea sysArea = context.getSysArea();
        JTextArea textArea3 = context.getTextArea3();
        JTextArea textField2 = context.getTextArea4();
        JLabel userLabe2 = context.getUserLabe2();
        JButton button3 = context.getButton3();
        sysArea.setText("---Game begins---\n");
        textArea3.setText("【landlord】 " +"\n");
        textArea3.setEditable(false);
        userLabe2.setVisible(true);
        userLabe2.setText("input");
        textField2.setVisible(true);
        textField2.setEditable(true);
        context.getButton1().setText("submit");
        button3.setText("pass");
        button3.setVisible(true);
        context.getButton2().setText("exit");
        try {
            //HttpClientPool.getHttpClient().get("http://localhost:8081/test/demo");
            PlayerDto player = new PlayerDto();
            player.setPlayerId("111");
            player.setRobot(false);
            player.setName("user");
            GameServiceImpl gameService = new GameServiceImpl();
            gameService.joinGame(player);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        eventEnum = EventEnum.POKER;
    }

    public static void passEvent() {
        if (eventEnum == EventEnum.POKER) {
            sendToServer("pass");
        }
    }

    public static void exitEvent() {
        showMenu();
    }
}
